package com.example.intermediate_control;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

import com.example.intermediate_control.util.ViewUtil;

/**
  *
  * @description 文本变更监听器demo测试
  * @param 
  * @return 
  * author snjgm
  * time 2022/7/14 上午11:18
  */
public class EditHideActivity extends AppCompatActivity{

    private EditText etTvPhone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_hide);
        etTvPhone = findViewById(R.id.et_tvPhone);
        EditText etTvPassword = findViewById(R.id.et_tvPassword);

        etTvPhone.addTextChangedListener(new HideTextWatcher(etTvPhone,11));
        etTvPassword.addTextChangedListener(new HideTextWatcher(etTvPassword,6));


    }
    //内部类的使用方式,本来该new一个TextWatcher,现在搞了个子类出来
    //定义一个编辑框监听器,在输入文本达到指定长度时自动隐藏输入法
    private class HideTextWatcher implements TextWatcher {
        //生命一个编辑框对象
        private EditText mView;
        //声明一个最大长度变量
        private int mMaxLength;
        public HideTextWatcher(EditText etTvPhone, int maxLength) {
            this.mView = etTvPhone;
            this.mMaxLength = maxLength;
        }

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            //获得已输入的文本字符
            String str = editable.toString();
            //进行逻辑判断后,隐藏键盘
            if(str.length() == mMaxLength){
                ViewUtil.hideOneInputMethod(EditHideActivity.this,mView);
            }
        }
    }
}